const nodemailer = require("nodemailer")
const config = require("./config");
const { promises } = require("nodemailer/lib/xoauth2");

/**
 * 创建发送邮件的代理
 */

let transport = nodemailer.createTransport({
    host:'smtp.163.com',
    port:465,
    secure:true,
    auth:{
        user:config.email_user, //发送方邮箱地址
        pass:config.email_pass  //邮箱授权码或密码
    }
});

/**
 * 发送邮件的函数
 * @param {*} _mailOptions 发送邮件的参数 
 * @returns
 */

function SendMail(_mailOptions){
    return new Promise(function(resolve,reject){
        transport.sendMail(_mailOptions,function(error,info){
            if(error){
                console.log(error);
                reject(error); //发送邮件失败时 外部调用该函数异常
            }else{
                console.log("邮件成功发送:"+info.response);
                resolve(info.response);
            }
        
        });
});
    }

module.exports.SendMail = SendMail